import { hiyooo } from "../../HiYOOO";
import { IStorageSecurity } from "./IStorageSecurity";

export class StorageSecuritySimple implements IStorageSecurity {

    private securityKey:string = null!;
    constructor(){
        const key  = hiyooo.config.game.localDataKey;
        const iv  = hiyooo.config.game.localDataIv;
        this.securityKey = key + iv;
    }   
 
        /**
     * 解密字符串
     * @param str - 要解密的字符串
     * @returns 解密后的字符串
     */
    decrypt(str: string): string {
        // 初始化一个空字符串用于存储解密后的结果
        let decrypted = "";
        // 遍历字符串中的每个字符
        for (let i = 0; i < str.length; i++) {
            // 将当前字符与 securityKey 中对应位置的字符进行异或操作，并将结果转换为字符
            decrypted += String.fromCharCode(str.charCodeAt(i) ^ this.securityKey.charCodeAt(i % this.securityKey.length));
        }
        // 返回解密后的字符串
        return decrypted;
    }
    /**
     * 加密字符串
     * @param str - 要加密的字符串
     * @returns 加密后的字符串
     */ 

    encrypt(str: string): string {
       let encrypted = "";
       for (let i = 0; i < str.length; i++) {
           encrypted += String.fromCharCode(str.charCodeAt(i) ^ this.securityKey.charCodeAt(i % this.securityKey.length));
       }    
       return encrypted;
    }
    encryptKey(key: string): string {
        return this.encrypt(key);
    }
     
}